<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
*/

/**
 * Description of model_transaksi
 *
 * @author Hadi
 */
class model_kas_penjualan extends Default_My_Model {

    function __construct() {
        parent::__construct();
    }

    function getQueryKasPenjualan($whr, $offset = 0, $length = 30, $only_row_count = false) {
        if ($only_row_count) {
            $field_list = "count(k.id_dp) as jumlah";
        } else {
            $field_list = "k.*, k.nama_item, mk.nama as nama_karyawan, jkp.nama_jkp, sjkp.nama_sjkp";
        }

        if ($length > 0){
            $batas = "LIMIT $length OFFSET $offset";
        }

        return "SELECT ".$field_list."
                FROM kas_penjualan k                
                LEFT JOIN mt_karyawan mk ON k.submitter_dp = mk.nik
                LEFT JOIN sub_jenis_kas_penjualan sjkp on sjkp.id_sjkp = k.id_sjkp
                LEFT JOIN jenis_kas_penjualan jkp on jkp.id_jkp = sjkp.id_jkp
                $whr ORDER by k.tanggal_dp desc ".$batas;
    }

    function getKasPenjualanData($whr, $offset = 0, $length= 30) {
        $sql = $this->getQueryKasPenjualan($whr, $offset, $length);
        return $this->db->query($sql);
    }

    function getKasPenjualanDataRC($whr) {
        $sql = $this->getQueryKasPenjualan($whr, 0, 1, true);
        $resultSet = $this->db->query($sql);
        $result = $resultSet->row();
        return $result->jumlah;
    }
    
    function getItemInvoice($idInvoice) {
        $sql = "select * from data_invoice_to_cust_item where id_invoice=?";
        $resultSet = $this->db->query($sql,array($idInvoice));        
        return $resultSet;
    }
    
    
    function getItemInvoicePerjanjian($idInvoice) {
        $sql = "select * from data_invoice_to_cust_perjanjian_item where id_invoice=?";
        $resultSet = $this->db->query($sql,array($idInvoice));        
        return $resultSet;
    }

    function getDiskonInvoice($idInvoice) {
        return $this->general_model->getScalar("diskon", 'data_invoice_to_cust', 'id_invoice', $idInvoice);
    }

    function getHargaItem($id_item, $jenis_harga, $use_dollar) {
        if ($use_dollar == "$") {
            return $this->general_model->getScalar($jenis_harga."_dollar", 'master_item', 'id_item', $id_item);
        } else {
            return $this->general_model->getScalar($jenis_harga, 'master_item', 'id_item', $id_item);
        }
    }

    function insertKasPenjualan($data) {
        $set = array(
                "tanggal_dp" => $data["tanggal_dp"],
                "nama_item" => $data["nama_item"],
                "item_dp" => $data["id_item"],
                "quantity_dp" => $data["quantity_dp"],
                "keterangan" => $data["keterangan"],
                "currency" => $data["currency"],
                "diskon_dp" => $data["diskon_dp"],
                "id_sjkp" => $data["id_sjkp"],
                "nama_item" => $data["nama_item"],
                "submitter_dp" => $data["submitter_dp"],
                "date_created_dp" => $data["date_created_dp"],
                "harga_beli_dp" => $data["harga_beli_dp"],
                "harga_jual_dp" => $data["harga_jual_dp"],
                "reff_tabel" => $data["reff_tabel"],
                "reff_id" => $data["reff_id"]
        );
        $this->db->insert("kas_penjualan", $set);
        return $this->db->insert_id();
    }

    function updateKasPenjualan($data, $id) {
        $set = array(
                "tanggal_dp" => $data["tanggal_dp"],
                "item_dp" => $data["id_item"],
                "nama_item" => $data["nama_item"],
                "quantity_dp" => $data["quantity_dp"],
                "keterangan" => $data["keterangan"],
                "currency" => $data["currency"],
                "diskon_dp" => $data["diskon_dp"],
                "id_sjkp" => $data["id_sjkp"],
                "submitter_dp" => $data["submitter_dp"],
                "date_created_dp" => $data["date_created_dp"],
                "harga_beli_dp" => $data["harga_beli_dp"],
                "harga_jual_dp" => $data["harga_jual_dp"]
        );
        $this->db->update("kas_penjualan", $set, "id_dp = ".$id);
    }

    function removeDataKasPenjualan($id) {
        $this->db->where('id_dp', $id);
        $this->db->delete('kas_penjualan', $where);
    }

}

?>
